package io.scnb.lesson003.demo1.test0.userregister;

import io.scnb.lesson003.demo1.test0.event.EventListener;
import io.scnb.lesson003.demo1.test0.event.EventMulticaster;
import io.scnb.lesson003.demo1.test0.event.SimpleEventMulticaster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ComponentScan
public class MainConfig0 {

    /**
     * 注册一个bean：事件发布者
     * 将容器中所有的事件监听器注入进来，放到EventMulticaster中
     */
    @Bean   
    @Autowired(required = false)
    public EventMulticaster multicaster(List<EventListener> eventListeners) {
        SimpleEventMulticaster eventPublisher = new SimpleEventMulticaster();
        if (eventListeners != null) {
            eventListeners.forEach(eventPublisher::addEventListener);
        }
        return eventPublisher;
    }

    /**
     * 注册一个bean：用户注册服务
     */
    @Bean
    public UserRegisterService userRegisterService(EventMulticaster eventMulticaster) {
        UserRegisterService userRegisterService = new UserRegisterService();
        userRegisterService.setEventMulticaster(eventMulticaster);
        return userRegisterService;
    }
}
